package com.chen.cloudpicture.manager.websocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * WebSocket 配置（定义连接）
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    //  WebSocket 事件处理器（分发器）
    @Resource
    private PictureEditHandler pictureEditHandler;
    // WebSocket 连接拦截器（进行权限校验）
    @Resource
    private WsHandshakeInterceptor wsHandshakeInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(pictureEditHandler, "/ws/picture/edit") // 定义事件处理器与连接请求地址
                .addInterceptors(wsHandshakeInterceptor) // 定义连接拦截器
                .setAllowedOrigins("*"); // 允许请求的跨域范围
    }
}
